﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExcelRefresh.Excel
{
    public class FileToRefresh : IComparable<FileToRefresh>, IEquatable<FileToRefresh>
    {
        public string FilePath { get; set; }
        public string Schedule { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public TimeSpan Duration { get { return EndTime - StartTime; } }
        public Exception ExceptionThrown { get; set; }
        public string RootPath { get; set; }

        public FileToRefresh(string filePath)
        {
            FilePath = filePath;
            var attributes = new ShellFileProperties(filePath);
            Schedule = attributes.ContentStatus.Trim().ToLowerInvariant();
        }

        public static implicit operator string(FileToRefresh other)
        {
            return other.FilePath;
        }

        public override string ToString()
        {
            return FilePath;
        }

        public override int GetHashCode()
        {
            return FilePath.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return FilePath.Equals(obj.ToString());
        }

        public int CompareTo(FileToRefresh other)
        {
            return this.FilePath.CompareTo(other.FilePath);
        }

        public bool Equals(FileToRefresh other)
        {
            return this.FilePath.Equals(other.FilePath);
        }

        public bool IsStale
        {
            get
            {
                //TODO: Replace with logic that inspects pivot tables RefreshDate

                if (string.IsNullOrEmpty(Schedule)) return false;
                var mode = Schedule.ToLowerInvariant().Replace(" ","").Split(';').FirstOrDefault();
                var now = DateTime.Now;
                var lastWriteTime = File.GetLastWriteTime(FilePath);
                var fileAge = now - lastWriteTime;
                var todayName = Enum.GetName(typeof(DayOfWeek), now.DayOfWeek).ToLowerInvariant();

                switch (mode)
                {
                    case "always": return true;

                    case "hourly":
                        return (fileAge.TotalMinutes > 59);

                    case "daily":
                        return lastWriteTime.Date != DateTime.Today;

                    case "weekly":
                        return (fileAge.Days > 6);

                    case "monthly":
                        return (fileAge.Days > 28 && now.Day == 1);

                    case "endofmonth":
                        return (lastWriteTime.Date != DateTime.Today
                            && DateTime.Today.Month != DateTime.Today.AddDays(1).Month);

                    case "startofmonth":
                        return (lastWriteTime.Date != DateTime.Today
                             && DateTime.Today.Month != DateTime.Today.AddDays(-1).Month);
                }

                if (mode == todayName && lastWriteTime.Date != DateTime.Today)
                    return true;

                return false;
            }
        }

        public BackupStrategy BackupType
        {
            get
            {
                if (string.IsNullOrEmpty(Schedule))
                    return BackupStrategy.None;

                var backup = Schedule
                    .ToUpperInvariant()
                    .Replace("-", "")
                    .Replace(" ", "")
                    .Split(';')
                    .Where(c => c.StartsWith("ARCHIVE"))
                    .FirstOrDefault();

                if (!string.IsNullOrEmpty(backup))
                    switch (backup)
                    {
                        case "ARCHIVESUBFOLDER":
                            return BackupStrategy.SubFolder;

                        case "ARCHIVESAMEFOLDER":
                            return BackupStrategy.SameFolder;
                    }
                return BackupStrategy.None;
            }
        }
        
        public Int32 Priority
        {
            get
            {
                if (string.IsNullOrEmpty(Schedule))
                    return 50;

                var backup = Schedule
                    .ToUpperInvariant()
                    .Replace("-", "")
                    .Replace(" ", "")
                    .Split(';')
                    .Where(c => c.EndsWith("PRIORITY"))
                    .FirstOrDefault();

                if (!string.IsNullOrEmpty(backup))
                    switch (backup)
                    {
                        case "MAXIMUMPRIORITY":
                            return 100;

                        case "VERYHIGHPRIORITY":
                            return 90;

                        case "HIGHPRIORITY":
                            return 75;

                        case "LOWPRIORITY":
                            return 30;

                        case "LOWESTPRIORITY":
                            return 10;
                    }

                return 50;
            }
        }
  
    
    }


}
